package cn.fyg.service.finance4wx.handler;

import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.WxCpXmlMessage;
import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage;
import org.springframework.stereotype.Component;

import cn.fyg.service.finance4wx.builder.TextBuilder;
import cn.fyg.service.finance4wx.utils.JsonUtils;

import java.io.StringReader;
import java.util.Map;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.internal.Streams;
import com.google.gson.stream.JsonReader;

/**
 * * @author Binary Wang(https://github.com/binarywang)
 */
@Component
public class MsgHandler extends AbstractHandler {

    @Override
    public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage,
                                    Map<String, Object> context, WxCpService weixinService,
                                    WxSessionManager sessionManager) {

        if (!wxMessage.getMsgType().equals(WxConsts.XML_MSG_EVENT)) {
            //TODO 可以选择将消息保存到本地
        }

        //TODO 组装回复消息
        //String content = "收到信息内容：" + JsonUtils.toJson(wxMessage);
        String content = JsonUtils.toJson(wxMessage);
        JsonElement je = Streams.parse(new JsonReader(new StringReader(content)));
		JsonObject jo = je.getAsJsonObject();
		String rmsg = jo.get("content").toString();
		String msg = "试试输出：资金";
		if (rmsg.equals("\"资金\"")) {
//			String url = "http:\\\\172.22.1.82:7777\\login\\";
			String url = "http:\\\\106.15.219.19:8001\\web-finance\\login\\";
			msg = "<a href = \'" + url +"\'>资金秘书</a>";
		}
		
        return new TextBuilder().build(msg, wxMessage, weixinService);

    }

}
